import { getToken } from '@/utils/auth';

enum Api {
  exportDeviceDayPowerHistory = '/data/calc_power/export-day-power',
  exportDeviceMonthPowerHistory = '/data/calc_power/export-month-power',
}

export function exportDeviceDayPowerHistoryApi(data: {
  sn: string;
  startTs: number;
  endTs: number;
  fileType: 'xlsx';
}) {
  const token = getToken();
  fetch('/hc-api' + Api.exportDeviceDayPowerHistory, {
    method: 'POST',
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      Authorization: token,
    },
  })
    .then(function (response: Response) {
      return response.blob();
    })
    .then(function (blob) {
      const link = document.createElement('a');
      link.style.display = 'none';
      link.href = URL.createObjectURL(blob);
      link.download = 'a.xlsx';
      document.body.appendChild(link);
      link.click();
      // 释放的 URL 对象以及移除 a 标签
      URL.revokeObjectURL(link.href);
      document.body.removeChild(link);
    });
}

export function exportDeviceMonthPowerHistoryApi(data: {
  sn: string;
  startTs: number;
  endTs: number;
  fileType: 'xlsx';
}) {
  const token = getToken();
  fetch('/hc-api' + Api.exportDeviceMonthPowerHistory, {
    method: 'POST',
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      Authorization: token,
    },
  })
    .then(function (response: Response) {
      return response.blob();
    })
    .then(function (blob) {
      const link = document.createElement('a');
      link.style.display = 'none';
      link.href = URL.createObjectURL(blob);
      link.download = 'a.xlsx';
      document.body.appendChild(link);
      link.click();
      // 释放的 URL 对象以及移除 a 标签
      URL.revokeObjectURL(link.href);
      document.body.removeChild(link);
    });
}
